package edu.nyupoly.wzj;

import java.util.Random;

public class ComputerPlayer extends Player{

	public ComputerPlayer(String name,Mark mark) {
		super(name,mark);
	}
	
	/*
	 *  The worst situation: there is only one empty cell in this board.
	 *  9 cases for this situation
	 *  
	 *  Expectation of getting the empty cell=9*(Pr(hit in 1st time)+Pr(hit in 2nd time)+ ...)
	 *  = 9*[ (1/9) + (8/9)(1/9) + (8/9)^2(1/9) + ... ]
	 *  = 1*[1 + (8/9) + (8/9)^2 + ...]
	 *  = 1/[1-(8/9)]
	 *  = 9 = O(n)
	 *  
	 *  n: how many cells in the board.
	 *  
	 *  
	 *  This approach is not worse than other approaches.
	 *  ( for example, cell[0][0] => cellB[1]
	 *  	cell[0][1] => cellB[2]
	 *  	cell[0][2] => cellB[3]
	 *  	cell[1][0] => cellB[4]
	 *  	cell[1][1] => cellB[5]
	 *      cell[1][2] => cellB[6]
	 *      cell[2][0] => cellB[7]
	 *      cell[2][1] => cellB[8]
	 *      cell[2][2] => cellB[9]
	 *      
	 *   Every time you move, you need to traverse the whole board to check and put empty cells
	 *   into an ArrayList. Then making a Random index for this ArrayList.
	 *   Every nextMove's running time for this approach is Big Theta(n).
	 *  
	 *  
	 */
	
	public void makeMove(Board b) {
		
		Random rd1 = new Random(System.currentTimeMillis());  // The seed is current time.
		Random rd2 = new Random(System.currentTimeMillis()+ 37);//new Random().nextInt(3757));  
		// The seed is current time plus a random number.
		
		int row;
		int col;
		
		do {
			row = rd1.nextInt(3);
			col = rd2.nextInt(3);
		}
		while(b.cells[row][col].claimed);
		
		b.setPiece(row, col, this);
		
		//this.attempt++;
		
	}
	
	public boolean isWinner(Board b) {
		return b.hasWon(this.myMark);
	}

}
